package com.hthy.futures.ui.fragment;

import android.os.Build;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.hthy.futures.R;
import com.hthy.futures.api.ST_TradeApi;
import com.hthy.futures.base.BaseFragment;
import com.hthy.futures.utils.ErrorUtils;
import com.hthy.futures.utils.LogUtils;
import com.vilyever.socketclient.SocketClient;

import net.hthy.bean.Trade;

import butterknife.Bind;

/**
 * Author:  Mr.bingley
 * Version:
 * Date:  2017/3/27
 */

public class NoticeContentFragment extends BaseFragment {
    @Bind(R.id.tv_notice)
    TextView mTvNotice;
    @Bind(R.id.webview)
    WebView mWebView;

    @Override
    protected int getLayoutId() {
        return R.layout.frg_notice_content;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);

        socketClient.sendData(ST_TradeApi.searchNoticeContent(NoticeFragment.noticeId));


        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
        } else {
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        }
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

    }

    @Override
    protected void onResponse(SocketClient client, String responMes, byte[] subBytes) {
        super.onResponse(client, responMes, subBytes);
        LogUtils.e(responMes);
        // 然后接着从返回的数据中截掉前面的37位，剩下就是json字符串内容。
        if (responMes.indexOf("2054") != -1) {

            try {
                Trade.QueryNoticeContentResponseMsg queryNoticeContentResponseMsg = Trade.QueryNoticeContentResponseMsg.parseFrom(subBytes);
                int iErrorCode = queryNoticeContentResponseMsg.getResponseMsg().getIErrorCode();
                if (0 == iErrorCode) {   // 0说明是正确返回的
                    String jsonElement = queryNoticeContentResponseMsg.getSsNoticeContent();
                    // 如果包含html,通过webview加载
                    if (jsonElement.contains("</")) {
                        mWebView.setVisibility(View.VISIBLE);
                        mTvNotice.setVisibility(View.GONE);
                        mWebView.loadDataWithBaseURL(null, jsonElement, "text/html", "utf-8", null);
                    } else {
                        mWebView.setVisibility(View.GONE);
                        mTvNotice.setVisibility(View.VISIBLE);
                        mTvNotice.setText(jsonElement);
                    }
                } else {
                    ErrorUtils.sendErrorMes(getActivity(), iErrorCode + "");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

}
